package com.yumijinfu.fragment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.yumijinfu.R;
import com.yumijinfu.base.LazyFragment;
import com.yumijinfu.entity.Info;
import com.yumijinfu.entity.Service;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 投资记录
 */
public class TouZiJiLuFragment extends LazyFragment {

    @Bind(R.id.listview)
    ListView listview;
    private TouZiJiLuAdapter mTouZiJiLuAdapter;

    @Override
    public View initView() {

        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);                    //第一个参数布局文件
        View view = inflater.inflate(R.layout.fragment_tou_zi_ji_lu, null);
//        listview = (ListView) view.findViewById(R.id.listview);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void initData() {


        if (mTouZiJiLuAdapter == null) {
            //1.获取网络数据
            Toast.makeText(getActivity(), "加载了投资记录数据", Toast.LENGTH_SHORT).show();


            //2.
            mTouZiJiLuAdapter = new TouZiJiLuAdapter(getActivity(), Service.getdata(50));
            listview.setAdapter(mTouZiJiLuAdapter);

        } else {
            Toast.makeText(getActivity(), "进行懒加载了投资记录数据", Toast.LENGTH_SHORT).show();
            mTouZiJiLuAdapter.notifyDataSetChanged();
        }

    }


    class TouZiJiLuAdapter extends BaseAdapter {

        private List<Info> infos;
        private Context context;
        private LayoutInflater layoutInflater;

        public TouZiJiLuAdapter(Context context, List<Info> infos) {
            this.infos = infos;
            this.context = context;
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return infos.size();
        }

        @Override
        public Object getItem(int position) {
            return infos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            ViewHolder viewHolder = null;

            if (viewHolder == null) {
                viewHolder = new ViewHolder();
                convertView = layoutInflater.inflate(R.layout.touzijilu_item, null);
                viewHolder.tv_money = (TextView) convertView.findViewById(R.id.tv_money);
                viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
                viewHolder.tv_number = (TextView) convertView.findViewById(R.id.tv_number);
                viewHolder.tv_xiaoyi = (TextView) convertView.findViewById(R.id.tv_xiaoyi);
                viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);


                convertView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            Info info = infos.get(position);

            viewHolder.tv_number.setText(info.getName());


            return convertView;
        }


    }

    public static final class ViewHolder {
        TextView tv_number;
        TextView tv_name;
        TextView tv_money;
        TextView tv_xiaoyi;
        TextView tv_time;

    }

}
